package com.sourcemuch.im.caiba;

import android.content.Context;
import android.media.AudioManager;
import android.os.Build;
import android.util.Log;

import androidx.core.app.NotificationManagerCompat;

import java.util.HashMap;
import java.util.Objects;

import cn.wildfire.chat.app.extendsion.classes.Textual;
import cn.wildfire.chat.app.extendsion.utils.TextUtils;
import cn.wildfire.chat.app.main.MeFragment;
import cn.wildfire.chat.kit.contact.ContactListFragment;
import cn.wildfire.chat.kit.conversationlist.ConversationGroupListFragment;
import cn.wildfire.chat.kit.conversationlist.ConversationListFragment;
import cn.wildfirechat.model.UserInfo;

/**
 * Create by HEKE, 2019/9/10 14:40
 * TODO 手动连接操作全局对象
 */
public class ManumotiveIBinder {

    /**
     * 消息界面碎片
     */
    public static final String PAGE_SINGLE = "page_single";

    /**
     * 联系人界面碎片
     */
    public static final String PAGE_CONTACT = "page_contact";

    /**
     * 群组界面碎片
     */
    public static final String PAGE_GROUP = "page_group";

    /**
     * 我的界面碎片
     */
    public static final String PAGE_ME = "page_me";

    /**
     * 群组界面刷新对象
     */
    public static final String GROUP_LIST_VIEWMODEL = "group_list_conversation";

    private Context context;
    private HashMap<String, Object> objectsHashMap;

    public ManumotiveIBinder() {
        context = CaiBaChatApp.getContext();
        objectsHashMap = new HashMap<>();
    }

    /**
     * 绑定对象
     *
     * @param key
     * @param object
     */
    public void bindObject(String key, Object object) {
        if (object == null) {
            return;
        }
        objectsHashMap.put(key, object);
    }

    /**
     * 根据key值获取全局对象
     *
     * @param key
     */
    public Object getObject(String key) {
        return objectsHashMap.get(key);
    }

    /**
     * 刷新Tab页面数据显示
     *
     * @param type   刷新页面类型，主界面下的tab对应项
     * @param delays 延时刷新时长
     */
    public void RefreshPageDatas(int type, int delays) {
        postRun(new Runnable() {
            @Override
            public void run() {
                switch (type) {
                    case Textual.TYPE_CONVERSATION:
                        ((ConversationListFragment) Objects.requireNonNull(objectsHashMap.get(PAGE_SINGLE))).refreshConversations();
                        break;
                    case Textual.TYPE_CONTACT:
                        ((ContactListFragment) Objects.requireNonNull(objectsHashMap.get(PAGE_CONTACT))).loadContacts(true);
                        break;
                    case Textual.TYPE_GROUP_CONVERSATION:
                        ((ConversationGroupListFragment) Objects.requireNonNull(objectsHashMap.get(PAGE_GROUP))).refreshConversations();
                        break;
                    case Textual.TYPE_ME:
                        ((MeFragment) Objects.requireNonNull(objectsHashMap.get(PAGE_ME))).handleUpdateUserInfo();
                        ((MeFragment) Objects.requireNonNull(objectsHashMap.get(PAGE_ME))).setClearCacheSize();
                        break;
                    default:
                        break;
                }
            }
        }, delays);
    }

    /**
     * 根据用户所有的备注、昵称、群昵称显示判断显示不同内容
     *
     * @param userInfo
     * @return
     */
    public static String getPreferential(UserInfo userInfo) {
        if (userInfo == null) {
            return null;
        }
        if (!TextUtils.isTextEmpty(userInfo.friendAlias)) {
            return userInfo.friendAlias;
        }
        if (!TextUtils.isTextEmpty(userInfo.groupAlias)) {
            return userInfo.groupAlias;
        }
        return userInfo.displayName;
    }

    /**
     * Post操作
     *
     * @param runnable
     * @param delays
     */
    public void postRun(Runnable runnable, int delays) {
        if (delays == 0) {
            CaiBaChatApp.getMainHandler().post(runnable);
        } else {
            CaiBaChatApp.getMainHandler().postDelayed(runnable, delays);
        }
    }

    /**
     * 设置听筒或者免提模式
     *
     * @param handset 是否听筒模式
     */
    public void setSoundSourceSpeakModel(boolean handset) {
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        int currVolume = audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL);
        Log.e("##### ", "setSoundSourceSpeakModel  currVolume : " + currVolume + "   handset : " + handset + "  isSpeakOn : " + audioManager.isSpeakerphoneOn());
        if (!handset) {
            audioManager.setMode(AudioManager.MODE_NORMAL);
            audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL,
                    audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL),
                    AudioManager.STREAM_VOICE_CALL);
            audioManager.setSpeakerphoneOn(true);
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
            } else {
                audioManager.setMode(AudioManager.MODE_IN_CALL);
            }
            audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, currVolume,
                    AudioManager.STREAM_VOICE_CALL);
            audioManager.setSpeakerphoneOn(false);
        }
    }

    /**
     * 判断当前程序是否开启应用通知，锁屏通知等功能
     */
    public boolean isNotificationEnabled(Context context) {
        boolean isOpened = false;
        try {
            isOpened = NotificationManagerCompat.from(context).areNotificationsEnabled();
        } catch (Exception e) {
            e.printStackTrace();
            isOpened = false;
        }
        return isOpened;
    }

    /**
     * 释放捆绑操作
     */
    public void release() {
        objectsHashMap.clear();
    }

}
